package com.xiaoshuidi.cloud.module.member.controller.admin.feedback;


import com.xiaoshuidi.cloud.framework.common.pojo.CommonResult;
import com.xiaoshuidi.cloud.framework.common.pojo.PageResult;
import com.xiaoshuidi.cloud.framework.excel.core.util.ExcelUtils;
import com.xiaoshuidi.cloud.module.member.controller.admin.feedback.vo.*;
import com.xiaoshuidi.cloud.module.member.convert.feedback.MemberFeedbackConvert;
import com.xiaoshuidi.cloud.module.member.enums.ApiConstants;
import com.xiaoshuidi.cloud.module.member.service.feedback.MemberFeedbackService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;

import java.io.IOException;
import java.util.List;

import static com.xiaoshuidi.cloud.framework.common.pojo.CommonResult.success;

/**p>
 * 意见反馈 前端控制器
 * </p>
 *
 * @author hans.zhang
 * @since 2023-11-28
 */
@Slf4j
@Tag(name = "后台管理 - 意见反馈")
@RestController
@RequestMapping(ApiConstants.NAME + "/member-feedback")
@RequiredArgsConstructor
public class MemberFeedbackController {

    private final MemberFeedbackService memberFeedbackService;

    @GetMapping("/page")
    @Operation(summary = "分页查询")
    @PreAuthorize("@ss.hasPermission('member:member-feedback:query')")
    public CommonResult<PageResult<MemberFeedbackPageRespVO>> page(MemberFeedbackPageReqVO pageReqVO){
        return success(memberFeedbackService.feedbackPage(pageReqVO));
    }

    @GetMapping("/get")
    @Operation(summary = "查看")
    @PreAuthorize("@ss.hasPermission('member:member-feedback:query')")
    public CommonResult<MemberFeedbackGetRespVO> get(@NotNull(message = "id不能为空") Long feedbackId){
        return success(memberFeedbackService.get(feedbackId));
    }

    @PostMapping("/handle")
    @Operation(summary = "处理")
    @PreAuthorize("@ss.hasPermission('member:member-feedback:update')")
    public CommonResult<Boolean> handle(@Valid @RequestBody MemberFeedbackHandleReqVO handleReqVO){
        return success(memberFeedbackService.handle(handleReqVO));
    }

    @PostMapping("/assign-apartment")
    @Operation(summary = "分配门店")
    @PreAuthorize("@ss.hasPermission('member:member-feedback:update')")
    public CommonResult<Boolean> assignApartment(@RequestBody @Valid AssignApartmentReqVO assignApartmentReqVO){
        return success(memberFeedbackService.assignApartment(assignApartmentReqVO));
    }

    @GetMapping("/export")
    @PreAuthorize("@ss.hasPermission('member:member-feedback:export')")
    public void export(MemberFeedbackPageReqVO pageReqVO, HttpServletResponse response) throws IOException {
        List<MemberFeedbackPageRespVO> memberFeedbackPageRespVOS = memberFeedbackService.exportList(pageReqVO);
        List<MemberFeedbackExportVO> datas = MemberFeedbackConvert.convertToExport(memberFeedbackPageRespVOS);
        ExcelUtils.write(response, "意见反馈.xls", "数据", MemberFeedbackExportVO.class, datas);
    }
}
